{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import re\n",
    "import getopt\n",
    "import shutil\n",
    "import posixpath\n",
    "import lxml.etree\n",
    "import calendar, time\n",
    "import urllib.request as urllib2\n",
    "import geopandas as gpd\n",
    "import pandas as pd\n",
    "from multiprocessing import Pool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/jovyan/topohack/contributors/willkochtitzky/ArcticDEM/ArcticDEM/ArcticDEM\n",
      "--2019-06-21 18:37:34--  http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/indexes/ArcticDEM_Tile_Index_Rel7.zip\n",
      "Resolving data.pgc.umn.edu (data.pgc.umn.edu)... 134.84.212.106\n",
      "Connecting to data.pgc.umn.edu (data.pgc.umn.edu)|134.84.212.106|:80... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 538740 (526K) [application/zip]\n",
      "Saving to: ‘ArcticDEM_Tile_Index_Rel7.zip’\n",
      "\n",
      "ArcticDEM_Tile_Inde 100%[===================>] 526.11K  2.60MB/s    in 0.2s    \n",
      "\n",
      "2019-06-21 18:37:35 (2.60 MB/s) - ‘ArcticDEM_Tile_Index_Rel7.zip’ saved [538740/538740]\n",
      "\n",
      "Archive:  ArcticDEM_Tile_Index_Rel7.zip\n",
      "  inflating: ArcticDEM_Tile_Index_Rel7.dbf  \n",
      "  inflating: ArcticDEM_Tile_Index_Rel7.prj  \n",
      "  inflating: ArcticDEM_Tile_Index_Rel7.shp  \n",
      "  inflating: ArcticDEM_Tile_Index_Rel7.shx  \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_1_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_1_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_1_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_1_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_40/10_40_2_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_40/10_40_2_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_1_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_1_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_2_1_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_2_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_2_1_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_2_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_2_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_41/10_41_2_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_2_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_41/10_41_2_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/08_40/08_40_2_1_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/08_40/08_40_2_1_2m_v3.0.tar.gz (new)\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_2_2m_v3.0.tar.gz --> \n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/10_42/10_42_1_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_2_2m_v3.0.tar.gz (new)\n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/10_42/10_42_1_2_2m_v3.0.tar.gz (new)\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/2m/08_40/08_40_2_2_2m_v3.0.tar.gz --> \n",
      "\t/home/jovyan/topohack/data/ArcticDEM/ArcticDEM/08_40/08_40_2_2_2m_v3.0.tar.gz (new)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bbox=[-160,62,-150,63]\n",
    "dest_folder='/home/jovyan/topohack/data/ArcticDEM/ArcticDEM'\n",
    "fetch_arcticDEM(bbox,dest_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fetch_arcticDEM(bbox,dest_folder):\n",
    "    \n",
    "    #make a directory for ArcticDEM files\n",
    "    %mkdir ArcticDEM\n",
    "    %cd ArcticDEM/\n",
    "\n",
    "    #download the Arctic DEM tiles from PGC\n",
    "    !wget -nc http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/indexes/ArcticDEM_Tile_Index_Rel7.zip\n",
    "    !unzip ArcticDEM_Tile_Index_Rel7.zip\n",
    "\n",
    "    #define the bounding box that you want to download DEMs for\n",
    "    bbox_w = bbox[0]\n",
    "    bbox_s = bbox[1]\n",
    "    bbox_e = bbox[2]\n",
    "    bbox_n = bbox[3]\n",
    "    \n",
    "    #read in tiles from the ArcticDEM\n",
    "    tiles = gpd.read_file(\"ArcticDEM_Tile_Index_Rel7.shp\")\n",
    "    tiles = tiles.to_crs({'init':'epsg:4326'}) #transform to WGS84\n",
    "\n",
    "    #intersect the \n",
    "    intersection = tiles.cx[bbox_w:bbox_e,bbox_s:bbox_n]\n",
    "\n",
    "    download_tiles_ids = intersection['tile'].values\n",
    "    dest_folder = [dest_folder]*len(download_tiles_ids)\n",
    "    input_var = list(zip(dest_folder,download_tiles_ids))\n",
    "    \n",
    "    with Pool(10) as p:\n",
    "        p.map(arcticDEM_download,[tile_id for tile_id in input_var])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Written by Tyler Sutterley (05/2019)\n",
    "\n",
    "# adapted and put into Jupyter by M. Truffer (06/2019)\n",
    "# adapted further by Will Kochtitzky and the #topohack team (06/2019)\n",
    "\n",
    "#Program to sync ArcticDEM tar files\n",
    "#http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic\n",
    "\n",
    "#-- PURPOSE: check internet connection\n",
    "def check_connection():\n",
    "    #-- attempt to connect to public http Polar Geospatial Center host\n",
    "    try:\n",
    "        urllib2.urlopen('http://data.pgc.umn.edu/elev/dem/', timeout=1)\n",
    "    except urllib2.URLError:\n",
    "        raise RuntimeError('Check internet connection')\n",
    "    else:\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-- PURPOSE: sync local ArcticDEM files with PGC public server\n",
    "def pgc_arcticdem_sync(DIRECTORY, VERSION, RESOLUTION, TILES=None,\n",
    "    LOG=False, LIST=False, CLOBBER=False, MODE=None):\n",
    "    #-- recursively create data directory if not presently existing\n",
    "    os.makedirs(DIRECTORY,MODE) if not os.path.exists(DIRECTORY) else None\n",
    "    #-- create log file with list of synchronized files (or print to terminal)\n",
    "    if LOG:\n",
    "        #-- output to log file\n",
    "        #-- format: PGC_ArcticDEM_sync_2002-04-01.log\n",
    "        today = time.strftime('%Y-%m-%d',time.localtime())\n",
    "        LOGFILE = 'PGC_ArcticDEM_sync_{0}.log'.format(today)\n",
    "        fid = open(os.path.join(DIRECTORY,LOGFILE),'w')\n",
    "        print('PGC ArcticDEM Sync Log ({0})'.format(today), file=fid)\n",
    "        print('VERSION={0}'.format(VERSION), file=fid)\n",
    "        print('RESOLUTION={0}'.format(RESOLUTION), file=fid)\n",
    "        print('TILES={0}'.format(','.join(TILES)), file=fid) if TILES else None\n",
    "    else:\n",
    "        #-- standard output (terminal output)\n",
    "        fid = sys.stdout\n",
    "\n",
    "    #-- remote http server for PGC DEM data\n",
    "    HOST = posixpath.join('http://data.pgc.umn.edu','elev','dem','setsm')\n",
    "    #-- compile regular expression operators for tiles\n",
    "    R1 = re.compile('|'.join(TILES)) if TILES else re.compile('\\d+_\\d+')\n",
    "    R2 = re.compile('(\\d+_\\d+)_(.*?)\\.tar\\.gz')\n",
    "    #-- compile regular expression operators for shapefiles\n",
    "    R3 = re.compile('(.*?)_Tile_Index_Rel(\\d+)\\.zip')\n",
    "\n",
    "    #-- compile HTML parser for lxml\n",
    "    parser = lxml.etree.HTMLParser()\n",
    "\n",
    "    #-- remote directory for data version and resolution\n",
    "    remote_dir = posixpath.join(HOST,'ArcticDEM','mosaic',VERSION,RESOLUTION)\n",
    "    #-- open connection with PGC server at remote directory\n",
    "    request = urllib2.Request(remote_dir)\n",
    "    response = urllib2.urlopen(request, timeout=20)\n",
    "    #-- read and parse request for files (names and modified dates)\n",
    "    tree = lxml.etree.parse(response, parser)\n",
    "    colnames = tree.xpath('//td[@class=\"indexcolname\"]//a/@href')\n",
    "    collastmod = tree.xpath('//td[@class=\"indexcollastmod\"]/text()')\n",
    "    remote_sub = sorted([d for i,d in enumerate(colnames) if R1.match(d)])\n",
    "    #-- for each tile subdirectory\n",
    "    for sd in remote_sub:\n",
    "        #-- check if data directory exists and recursively create if not\n",
    "        local_dir = os.path.join(DIRECTORY,sd)\n",
    "        if not os.access(local_dir, os.F_OK) and not LIST:\n",
    "            os.makedirs(local_dir,MODE)\n",
    "        #-- open connection with PGC server at remote directory\n",
    "        request = urllib2.Request(posixpath.join(remote_dir,sd))\n",
    "        response = urllib2.urlopen(request, timeout=20)\n",
    "        #-- read and parse request for files (names and modified dates)\n",
    "        tree = lxml.etree.parse(response, parser)\n",
    "        colnames = tree.xpath('//td[@class=\"indexcolname\"]//a/@href')\n",
    "        collastmod = tree.xpath('//td[@class=\"indexcollastmod\"]/text()')\n",
    "        remote_file_lines = [i for i,f in enumerate(colnames) if R2.match(f)]\n",
    "        #-- sync each ArcticDEM data file\n",
    "        for i in remote_file_lines:\n",
    "            #-- remote and local versions of the file\n",
    "            remote_file = posixpath.join(remote_dir,sd,colnames[i])\n",
    "            local_file = os.path.join(local_dir,colnames[i])\n",
    "            #-- get last modified date and convert into unix time\n",
    "            lastmodtime = time.strptime(collastmod[i].rstrip(),'%Y-%m-%d %H:%M')\n",
    "            remote_mtime = calendar.timegm(lastmodtime)\n",
    "            #-- sync ArcticDEM tar file\n",
    "            http_pull_file(fid, remote_file, remote_mtime, local_file, LIST,\n",
    "                CLOBBER, MODE)\n",
    "        #-- close request\n",
    "        request = None\n",
    "\n",
    "    #-- remote directory for shapefiles of data version\n",
    "    remote_dir = posixpath.join(HOST,'ArcticDEM','indexes')\n",
    "    #-- open connection with PGC server at remote directory\n",
    "    request = urllib2.Request(remote_dir)\n",
    "    response = urllib2.urlopen(request, timeout=20)\n",
    "    #-- read and parse request for files (names and modified dates)\n",
    "    tree = lxml.etree.parse(response, parser)\n",
    "    colnames = tree.xpath('//td[@class=\"indexcolname\"]//a/@href')\n",
    "    collastmod = tree.xpath('//td[@class=\"indexcollastmod\"]/text()')\n",
    "    remote_file_lines = [i for i,d in enumerate(colnames) if R3.match(d)]\n",
    "    #-- sync each ArcticDEM shapefile\n",
    "    for i in remote_file_lines:\n",
    "        #-- remote and local versions of the file\n",
    "        remote_file = posixpath.join(remote_dir,colnames[i])\n",
    "        local_file = os.path.join(DIRECTORY,colnames[i])\n",
    "        #-- get last modified date and convert into unix time\n",
    "        lastmodtime = time.strptime(collastmod[i].rstrip(),'%Y-%m-%d %H:%M')\n",
    "        remote_mtime = calendar.timegm(lastmodtime)\n",
    "        #-- sync ArcticDEM shapefile\n",
    "        http_pull_file(fid, remote_file, remote_mtime, local_file, LIST,\n",
    "            CLOBBER, MODE)\n",
    "\n",
    "    #-- close log file and set permissions level to MODE\n",
    "    if LOG:\n",
    "        fid.close()\n",
    "        os.chmod(os.path.join(DIRECTORY,LOGFILE), MODE)\n",
    "\n",
    "#-- PURPOSE: pull file from a remote host checking if file exists locally\n",
    "#-- and if the remote file is newer than the local file\n",
    "def http_pull_file(fid,remote_file,remote_mtime,local_file,LIST,CLOBBER,MODE):\n",
    "    #-- if file exists in file system: check if remote file is newer\n",
    "    TEST = False\n",
    "    OVERWRITE = ' (clobber)'\n",
    "    #-- check if local version of file exists\n",
    "    if os.access(local_file, os.F_OK):\n",
    "        #-- check last modification time of local file\n",
    "        local_mtime = os.stat(local_file).st_mtime\n",
    "        #-- if remote file is newer: overwrite the local file\n",
    "        if (remote_mtime > local_mtime):\n",
    "            TEST = True\n",
    "            OVERWRITE = ' (overwrite)'\n",
    "    else:\n",
    "        TEST = True\n",
    "        OVERWRITE = ' (new)'\n",
    "    #-- if file does not exist locally, is to be overwritten, or CLOBBER is set\n",
    "    if TEST or CLOBBER:\n",
    "        #-- Printing files transferred\n",
    "        print('{0} --> '.format(remote_file), file=fid)\n",
    "        print('\\t{0}{1}\\n'.format(local_file,OVERWRITE), file=fid)\n",
    "        #-- if executing copy command (not only printing the files)\n",
    "        if not LIST:\n",
    "            #-- Create and submit request. There are a wide range of exceptions\n",
    "            #-- that can be thrown here, including HTTPError and URLError.\n",
    "            request = urllib2.Request(remote_file)\n",
    "            response = urllib2.urlopen(request, timeout=20)\n",
    "            #-- chunked transfer encoding size\n",
    "            CHUNK = 16 * 1024\n",
    "            #-- copy contents to local file using chunked transfer encoding\n",
    "            #-- transfer should work properly with ascii and binary data formats\n",
    "            with open(local_file, 'wb') as f:\n",
    "                shutil.copyfileobj(response, f, CHUNK)\n",
    "            #-- keep remote modification time of file and local access time\n",
    "            os.utime(local_file, (os.stat(local_file).st_atime, remote_mtime))\n",
    "            os.chmod(local_file, MODE)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def arcticDEM_download (input_v):\n",
    "\n",
    "    DIRECTORY = input_v[0]\n",
    "    LIST = False\n",
    "    LOG = False\n",
    "    CLOBBER = False\n",
    "    #-- ArcticDEM parameters\n",
    "    VERSION = 'v3.0'\n",
    "    RESOLUTION = '2m'\n",
    "    # tile for Kaskawulsh\n",
    "    TILES = input_v[1]\n",
    "    #-- permissions mode of the local directories and files (number in octal)\n",
    "    MODE = 0o775\n",
    "\n",
    "\n",
    "    #-- check internet connection before attempting to run program\n",
    "    pgc_arcticdem_sync(DIRECTORY, VERSION, RESOLUTION, TILES=TILES,\n",
    "                   LIST=LIST, LOG=LOG, CLOBBER=CLOBBER, MODE=MODE)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
